package com.study.Service.Impl;

import com.common.Utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.Entity.Category;
import com.study.Mapper.CategoryMapper;
import com.study.Service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageInfo<Category> getCategoryList(Integer pageNum, Integer pageSize, String categoryName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> categoryList = categoryMapper.getCategoryList(categoryName);
        return new PageInfo<>(categoryList);
    }
    @Override
    public List<Category> getAllCategory() {
        List<Category> categoryList = categoryMapper.getAllCategory();
        return categoryList;
    }

    @Override
    public void addCategory(Category category) {
        String token = category.getToken();
        String username= JwtUtil.extractUsername(token);
        category.setCreateUser(username);
        category.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        category.setCategoryId(makeCategoryId());
        categoryMapper.addCategory(category);
    }

    private String makeCategoryId() {
        return "C" + System.currentTimeMillis();
    }

    @Override
    public void updateCategory(Category category) {
        String token = category.getToken();
        String username= JwtUtil.extractUsername(token);
        category.setUpdateUser(username);
        category.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        categoryMapper.updateCategory(category);
    }



    @Override
    public void deleteCategory(String categoryId) {
        categoryMapper.deleteCategory(categoryId);
    }


}
